package logic

import (
	"context"
	"goods_srv/global"
	"goods_srv/goods_proto"
	"goods_srv/model"
	"google.golang.org/grpc/codes"
	"google.golang.org/grpc/status"
)

// 分类品牌列表
func (g *GoodsServer) CategoryBrandList(ctx context.Context, in *goods_proto.CategoryBrandFilterRequest) (*goods_proto.CategoryBrandListResponse, error) {
	var categoryBrands []model.GoodsCategoryBrand
	CategoryBrandListResponse := goods_proto.CategoryBrandListResponse{}
	//查询总条数
	var count int64
	global.DB.Model(&model.GoodsCategoryBrand{}).Count(&count)
	CategoryBrandListResponse.Total = int32(count)
	//Preload预加载
	global.DB.Model(&model.GoodsCategoryBrand{}).Preload("Category").Preload("Brand").Scopes(Paginate(int(in.Pages), int(in.PagePerNums))).Find(&categoryBrands)
	var categoryResponse []*goods_proto.CategoryBrandResponse
	for _, brands := range categoryBrands {
		categoryResponse = append(categoryResponse, &goods_proto.CategoryBrandResponse{
			Id: brands.BaseModel.ID,
			Brand: &goods_proto.BrandInfoResponse{
				Id:   brands.Brand.ID,
				Name: brands.Brand.Name,
				Logo: brands.Brand.Logo,
			},
			Category: &goods_proto.CategoryInfoResponse{
				Id:             brands.Category.ID,
				Name:           brands.Category.Name,
				ParentCategory: brands.Category.ParentCategoryID,
				Level:          brands.Category.Level,
				IsTab:          brands.Category.IsTab,
			},
		})
	}
	CategoryBrandListResponse.Data = categoryResponse
	return &CategoryBrandListResponse, nil
}

// 根据分类获取品牌信息
func (g *GoodsServer) GetCategoryBrandList(ctx context.Context, in *goods_proto.CategoryInfoRequest) (*goods_proto.BrandListResponse, error) {
	var brandListResponse goods_proto.BrandListResponse
	var category model.Category
	if result := global.DB.First(&category, in.Id); result.RowsAffected == 0 {
		return nil, status.Errorf(codes.NotFound, "商品分类不存在")
	}
	var categoryBrands []model.GoodsCategoryBrand
	if result := global.DB.Where(&model.GoodsCategoryBrand{CategoryID: category.ID}).Preload("Brands").Find(&categoryBrands); result.RowsAffected > 0 {
		brandListResponse.Total = int32(result.RowsAffected)
	}
	var brandInfoResponses []*goods_proto.BrandInfoResponse

	for _, categoryBrand := range categoryBrands {
		brandInfoResponses = append(brandInfoResponses, &goods_proto.BrandInfoResponse{
			Id:   categoryBrand.Brand.ID,
			Name: categoryBrand.Brand.Name,
			Logo: categoryBrand.Brand.Logo,
		})
	}
	brandListResponse.Data = brandInfoResponses
	return &brandListResponse, nil
}

// 根据品牌获取分类信息
func (g *GoodsServer) GetBrandCategoryList(ctx context.Context, in *goods_proto.BrandRequest) (*goods_proto.CategoryListResponse, error) {
	var categoryListResponse goods_proto.CategoryListResponse
	var brand model.Brands
	if result := global.DB.First(&brand, in.Id); result.RowsAffected == 0 {
		return nil, status.Errorf(codes.NotFound, "商品品牌不存在")
	}
	var categoryBrands []model.GoodsCategoryBrand
	if result := global.DB.Where(&model.GoodsCategoryBrand{BrandsID: brand.ID}).Preload("Category").Find(&categoryBrands); result.RowsAffected > 0 {
		categoryListResponse.Total = int32(result.RowsAffected)
	}
	var categoryInfoResponses []*goods_proto.CategoryInfoResponse
	for _, categoryBrand := range categoryBrands {
		categoryInfoResponses = append(categoryInfoResponses, &goods_proto.CategoryInfoResponse{
			Id:             categoryBrand.Category.ID,
			Name:           categoryBrand.Category.Name,
			ParentCategory: categoryBrand.Category.ParentCategoryID,
			Level:          categoryBrand.Category.Level,
			IsTab:          categoryBrand.Category.IsTab,
		})
	}
	categoryListResponse.Data = categoryInfoResponses
	return &categoryListResponse, nil
}

// 创建分类品牌
func (g *GoodsServer) CreateCategoryBrand(ctx context.Context, in *goods_proto.CategoryBrandRequest) (*goods_proto.CategoryBrandResponse, error) {
	var category model.Category
	if result := global.DB.First(&category, in.CategoryId); result.RowsAffected == 0 {
		return nil, status.Errorf(codes.InvalidArgument, "商品分类不存在")
	}
	var brand model.Brands
	if result := global.DB.First(&brand, in.BrandId); result.RowsAffected == 0 {
		return nil, status.Errorf(codes.InvalidArgument, "商品品牌不存在")
	}
	categoryBrand := model.GoodsCategoryBrand{
		CategoryID: in.CategoryId,
		BrandsID:   in.BrandId,
	}
	tx := global.DB.Save(&categoryBrand)
	if tx.Error != nil {
		return nil, status.Errorf(codes.Internal, "创建品牌分类失败")
	}
	return &goods_proto.CategoryBrandResponse{Id: categoryBrand.ID}, nil
}

// 删除分类品牌
func (g *GoodsServer) DeleteCategoryBrand(ctx context.Context, in *goods_proto.CategoryBrandRequest) (*goods_proto.Empty, error) {
	if result := global.DB.Delete(&model.GoodsCategoryBrand{}, in.GetCategoryId()); result.RowsAffected == 0 {
		return nil, status.Errorf(codes.NotFound, "删除商品分类品牌失败")
	}
	return &goods_proto.Empty{}, nil
}

// 修改分类品牌
func (g *GoodsServer) UpdateCategoryBrand(ctx context.Context, in *goods_proto.CategoryBrandRequest) (*goods_proto.Empty, error) {
	var categoryBrand model.GoodsCategoryBrand
	if result := global.DB.First(&categoryBrand, in.Id); result.RowsAffected == 0 {
		return nil, status.Errorf(codes.InvalidArgument, "商品分类品牌不存在")
	}
	var category model.Category
	if result := global.DB.First(&category, in.CategoryId); result.RowsAffected == 0 {
		return nil, status.Errorf(codes.InvalidArgument, "商品分类不存在")
	}
	var brand model.Brands
	if result := global.DB.First(&brand, in.BrandId); result.RowsAffected == 0 {
		return nil, status.Errorf(codes.InvalidArgument, "商品品牌不存在")
	}
	categoryBrand.CategoryID = in.CategoryId
	categoryBrand.BrandsID = in.BrandId
	tx := global.DB.Updates(&categoryBrand)
	if tx.Error != nil {
		return nil, status.Errorf(codes.Internal, "修改品牌分类失败")
	}
	return &goods_proto.Empty{}, nil
}
